package com.just.demo.handler;

import com.just.demo.entity.Info;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;

@Component
public class TestHandler {

    public Mono<ServerResponse> echoName(ServerRequest request) {
        return request.bodyToMono(Info.class).map(Info::getName)
                .flatMap(name -> ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
                        .body(BodyInserters.fromObject("hello" + name)));
    }

    public RouterFunction<ServerResponse> routes(TestHandler testHandler) {
        return RouterFunctions.route(RequestPredicates.POST("/route"), testHandler::echoName);

    }
}
